﻿using PuppeteerSharp;
using System;
using System.Threading.Tasks;

namespace PuppeteerDemo
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var options = new LaunchOptions
            {
                Headless = false,
                DefaultViewport = null,//页面大小自适应
                Args = new string[] { "--start-maximized" }//浏览器最大化显示
            };
            Console.WriteLine("Downloading chromium");

            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);

            using IBrowser browser = await Puppeteer.LaunchAsync(options);
            using var page = await browser.NewPageAsync();
            //设置浏览器的页面大小
            //await page.SetViewportAsync(new ViewPortOptions
            //{
            //    Width = 1024,
            //    Height = 768,
            //});

            await page.GoToAsync("https://www.baidu.com");

            //获取页面html
            var html = await page.GetContentAsync();

            //获取焦点并输入文本
            await page.TypeAsync("#kw", "测试");
            //单击事件
            await page.ClickAsync("#su");

            //将页面保存为图片
            //await page.ScreenshotAsync(@"D:\1.png",
            //    new ScreenshotOptions() { FullPage = true, Type = ScreenshotType.Png });
            Console.ReadLine();
            Console.WriteLine("执行完成");
        }
    }
}
